package com.ebelter.bodyfatscale.ui.pager.main;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebelter.bodyfatscale.R;
import com.ebelter.bodyfatscale.common.Constants;
import com.ebelter.bodyfatscale.model.Birthday;
import com.ebelter.bodyfatscale.ui.activity.AboutActivity;
import com.ebelter.bodyfatscale.ui.activity.DeviceManagerActivity;
import com.ebelter.bodyfatscale.ui.activity.FindPswActivity;
import com.ebelter.bodyfatscale.ui.activity.LoginActivity;
import com.ebelter.bodyfatscale.ui.activity.MainActivity;
import com.ebelter.bodyfatscale.ui.activity.ModifyActivity;
import com.ebelter.bodyfatscale.ui.activity.ModifyBirthdayActivity;
import com.ebelter.bodyfatscale.ui.activity.ModifyHeightActivity;
import com.ebelter.bodyfatscale.ui.view.dialogs.TextTipDialog;
import com.ebelter.bodyfatscale.util.ScaleBaseUtils;
import com.ebelter.bodyfatscale.util.SpecialText;
import com.ebelter.bodyfatscale.util.StringUtils;
import com.ebelter.bodyfatscale.util.ToastUtil;
import com.ebelter.bodyfatscale.util.UserSpUtil;
import com.ebelter.btlibrary.btble.ble.BlueToothAgency;
import com.ebelter.btlibrary.util.SpUtil;
import com.ebelter.btlibrary.util.ULog;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 设置
 */

public class SetPager extends BasePagerMain {


    private static final String TAG= "SetPager";
    @BindView(R.id.page_set_user_iv)
    ImageView page_set_user_iv;

    @BindView(R.id.page_set_name_tv)
    TextView page_set_name_tv;

    @BindView(R.id.page_set_email_tv)
    TextView page_set_email_tv;

    @BindView(R.id.page_set_stature_tv)
    TextView page_set_stature_tv;

    @BindView(R.id.page_set_birthday_tv)
    TextView page_set_birthday_tv;


    @BindView(R.id.set_exit_tv)
    TextView set_exit_tv;



    public SetPager(Activity context) {
        super(context);
    }

    @Override
    protected int loadLayoutById() {
        return R.layout.pager_set;
    }

    @Override
    public void initViews() {

    }

    @Override
    public void initData(Object data) {

    }

    @Override
    public void switchUnitUpdate() {

    }

    private String name;
    private String email;
    private int height;
    private Birthday birthday;
    //0是男 1是女
    public void initBaseData(int sex,String name,String email,int height,Birthday birthday){
        setUserIcon(sex);
        setName(name);
        setEmail(email);
        setHeight(height);
        if (birthday!=null){
            this.birthday=birthday;
            setBirthday(birthday.getFatterDay());
        }

    }

    private void setUserIcon(int sex) {
        //0是男 1是女
        if (page_set_user_iv!=null){
            page_set_user_iv.setImageResource(sex==0?R.drawable.icon_male_set:R.drawable.icon_female_set);
        }

    }

    public void setName(String name){
        this.name=name;
        if (page_set_name_tv!=null){
            page_set_name_tv.setText(name);
        }
    }

    public void setEmail(String email){
        this.email=email;
        if (page_set_email_tv!=null){
            page_set_email_tv.setText(email);
        }
    }

    public void setHeight(int h){
        if (page_set_stature_tv!=null){
            height=h;
            String height2=h+"";

            if (!height2.contains("cm")){
                height2=height2+"cm";
            }

            page_set_stature_tv.setText(height2);
        }
    }

    public void setBirthday(String birthday){
        if (page_set_birthday_tv!=null){
            page_set_birthday_tv.setText(SpecialText.formatDateStampString0(ScaleBaseUtils.getTime(birthday)));
        }
    }

    @OnClick({ R.id.set_name_rl,R.id.set_email_rl,R.id.set_stature_rl,R.id.set_birthday_rl,
            R.id.set_passcode_rl,R.id.set_devicemanager_rl,R.id.set_clearuserinfo_rl,
            R.id.set_firnware_rl,R.id.set_about_rl,R.id.set_exit_tv})
    public void btClick(View view){
        switch (view.getId()){
            case R.id.set_name_rl:
                set_name_rl_click();
                break;
            case R.id.set_email_rl:
                set_email_rl_click();
                break;

            case R.id.set_stature_rl:
                set_stature_rl_click();
                break;

            case R.id.set_birthday_rl:
                set_birthday_rl_click();
                break;

            case R.id.set_passcode_rl:
                set_passcode_rl_click();
                break;

            case R.id.set_devicemanager_rl:
                set_devicemanager_rl_click();
                break;
            case R.id.set_clearuserinfo_rl:
                set_clearuserinfo_rl_click();
                break;
            case R.id.set_firnware_rl:
                set_firnware_rl_click();
                break;
            case R.id.set_about_rl:
                set_about_rl_click();
                break;
            case R.id.set_exit_tv:
                exit_click();
                break;

        }
    }

    //退出
    private void exit_click() {
        ULog.i(TAG,"----exit_click");
        UserSpUtil.removeAll();
        Intent loginIntent =new Intent(getActivity(), LoginActivity.class);
        getActivity().startActivity(loginIntent);
        getActivity().finish();
//        TextTipDialog exit=new TextTipDialog(getActivity(), StringUtils.getResStr(R.string.exit_tip));
//        exit.show();
//        exit.setListener(new TextTipDialog.ICancelOkBtListener() {
//            @Override
//            public void cancel(Dialog dialogView) {
//                dialogView.cancel();
//            }
//            @Override
//            public void ok(Dialog dialogView) {
//                UserSpUtil.removeAll();
//                Intent loginIntent =new Intent(getActivity(), LoginActivity.class);
//                getActivity().startActivity(loginIntent);
//                getActivity().finish();
//
//            }
//        });
    }

    //身高修改界面
    private void set_stature_rl_click() {
        ULog.i(TAG,"----set_stature_rl_click");
        Intent heighIntent=new Intent(getActivity(), ModifyHeightActivity.class);
        heighIntent.putExtra(ModifyHeightActivity.ORIGINAL_HEIGHT,height);
        getActivity().startActivityForResult(heighIntent,ModifyHeightActivity.REQUESTCODE);
    }

    private void set_passcode_rl_click() {
        ULog.i(TAG,"----set_passcode_rl_click");
        Intent intent=new Intent(getActivity(), FindPswActivity.class);
        getActivity().startActivity(intent);


    }

    private void set_devicemanager_rl_click() {

        ULog.i(TAG,"----set_devicemanager_rl_click");
        Intent intent=new Intent(getActivity(), DeviceManagerActivity.class);
        getActivity().startActivityForResult(intent,DeviceManagerActivity.REQUEST_CODE);
    }

    private void set_clearuserinfo_rl_click() {
        ULog.i(TAG,"----set_clearuserinfo_rl_click");
        if (!BlueToothAgency.getInstance().isBluetoothOpen()){
            ToastUtil.show(R.string.no_bluetooth_enabled);
            return;
        }
        if (!((MainActivity)getActivity()).scalebleIsConnect()){
            ToastUtil.show(R.string.do_not_attach_the_scale);
            return;
        }

        TextTipDialog tipDialog=new TextTipDialog(getActivity(),StringUtils.getResStr(R.string.del_all_user_tip));
        tipDialog.show();
        tipDialog.setListener(new TextTipDialog.ICancelOkBtListener() {
            @Override
            public void cancel(Dialog dialogView) {
                dialogView.cancel();
            }

            @Override
            public void ok(Dialog dialogView) {
                dialogView.cancel();
                SpUtil.writeLong(Constants.IUser.CLEAR_ALLUSER_INFO_TIME,System.currentTimeMillis());
                SpUtil.writeBoolean(Constants.IUser.CLEAR_ALLUSER_INFO_DIALOG_HASSHOW,false);
                ((MainActivity)getActivity()).requestDelAllUser();

            }
        });



    }

    private void set_firnware_rl_click() {
        ULog.i(TAG,"----set_firnware_rl_click");
    }

    private void set_birthday_rl_click() {
        ULog.i(TAG,"----set_birthday_rl_click");
        Intent birthdayI=new Intent(getActivity(), ModifyBirthdayActivity.class);
        birthdayI.putExtra(ModifyBirthdayActivity.ORIGINAL_BIRTHDAY,birthday);
        getActivity().startActivityForResult(birthdayI,ModifyBirthdayActivity.REQUESTCODE);
    }

    private void set_email_rl_click() {
        ULog.i(TAG,"----set_email_rl_click");
        openModifyActivity(ModifyActivity.MODIFY_EMAIL,email);
    }

    private void set_name_rl_click() {
        ULog.i(TAG,"----set_name_rl_click");
        openModifyActivity(ModifyActivity.MODIFY_NAME,name);
    }



    private void set_about_rl_click() {
        ULog.i(TAG,"----set_about_rl_click");
        Intent about=new Intent(getActivity(), AboutActivity.class);
        getActivity().startActivity(about);
    }

    private void openModifyActivity(int MODIFY_STYLE,String displayV){
        Intent modifyA=new Intent(getActivity(), ModifyActivity.class);
        modifyA.putExtra(ModifyActivity.MODIFY_STYLE,MODIFY_STYLE);
        modifyA.putExtra(ModifyActivity.DISPLAY_VALUE,displayV);
        getActivity().startActivityForResult(modifyA,100);
    }

}
